<?php


namespace app\api\controller;


use app\api\service\Token;
use app\BaseController as Controller;
use think\response\Json;

class BaseController extends Controller
{
    protected function checkExclusiveScope()
    {
        Token::needExclusiveScope();
    }

    protected function checkPrimaryScope()
    {
        Token::needPrimaryScope();
    }

    protected function checkSuperScope()
    {
        Token::needSuperScope();
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @param int|null $code 状态码
     * @param string $msg
     * @param array | object $data
     * @return Json
     */
    protected function renderData( string $msg = '', int $code = null, array|object $data = []): Json
    {
        is_null($code) && $code = config('status.success');
        return json([
            'data' => $data,
            'msg' => $msg,
            'code' => $code,
        ]);
    }

    /**
     * 返回操作成功json
     * @param string $msg
     * @param array|string $data
     * @return Json
     */
    protected function renderSuccess(array|object $data = [], string $msg = 'success'): Json
    {
        return $this->renderData($msg, config('status.success'), $data);
    }

    /**
     * 返回操作失败json
     * @param string $msg
     * @param array $data
     * @return Json
     */
    protected function renderFail(string $msg = 'fail',  array|object $data = []): Json
    {
        return $this->renderData($msg, 999, $data);
    }
}